package com.zhl.service.impl;

import com.zhl.dao.ArticleDao;
import com.zhl.dao.FavoriteDao;
import com.zhl.model.Article;
import com.zhl.model.Favorite;
import com.zhl.service.ArticleService;
import com.zhl.utils.page.PageEntity;
import com.zhl.utils.page.PageUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class ArticleServiceImpl implements ArticleService {

    ArticleDao articleDao = new ArticleDao();
    FavoriteDao favoriteDao = new FavoriteDao();

    @Override
    public PageEntity<Article> findArticles(String title,Integer currPage) {
        PageEntity<Article> pageEntity = new PageEntity<>();
        List<Article> data = articleDao.findArticles(title,(currPage - 1) * pageEntity.getPageSize(), pageEntity.getPageSize());
        Integer totalCount = articleDao.findAll();
        return PageUtil.getPageEntity(pageEntity, currPage, data, totalCount);
    }

    @Override
    public boolean addArticle(String title, String content,Integer id,String name) {
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBrowseCount(0);
        article.setPublishDate(new Date());
        article.setPublishRealName(name);
        article.setUserId(id);
        boolean save = articleDao.Save(article);
        return save;
    }

    @Override
    public Map<String, Object> findArticleById(Integer id) {
        return articleDao.findArticleById(id);
    }

    @Override
    public Integer updateCollect(Integer uId, Integer aId,String collect) {
        Integer a = 0;
        if (collect.equals("收藏文章")){
            Favorite favorite = new Favorite();
            favorite.setUId(uId);
            favorite.setAId(aId);
            favoriteDao.Save(favorite);
            a = 0;
        }else {
            articleDao.deleteCollect(uId,aId);
            a = 1;
        }
        return a;
    }

    @Override
    public List<Map<String,Object>> findArticleByUid(Integer uId,String title) {
        return articleDao.findArticleByUid(uId,title);
    }

}
